package cn.fsd.controller;

import cn.fsd.domain.User;
import cn.fsd.exception.AppException;
import cn.fsd.exception.GateWayExceptionCodeMsg;
import cn.fsd.mapper.UserMapper;
import cn.fsd.service.UserService;
import cn.fsd.util.Constants;
import cn.fsd.util.JwtTokenUtil;
import cn.fsd.util.MD5Utils;
import cn.fsd.util.RedisUtil;
import cn.fsd.vo.ResultBean;
import cn.fsd.vo.UserBean;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

  @Autowired
  UserMapper userMapper;
  @Autowired
  UserService userService;
  @Autowired
  RedisUtil redisUtil;
  @Autowired
  JwtTokenUtil jwtTokenUtil;

  @PostMapping("/login")
  @ApiOperation(value = "用户使用用户名密码登录")
  public ResultBean userLogin(@RequestBody User user) {
    if (StringUtils.isEmpty(user.getUser_name())) {
      throw new AppException(GateWayExceptionCodeMsg.USERNAME_EMPTY);
    }
    if (StringUtils.isEmpty(user.getPassword())) {
      throw new AppException(GateWayExceptionCodeMsg.PASSWORD_EMPTY);
    }

    User theUser = userMapper.selectByUsername(user.getUser_name());

    if (theUser == null) {
      throw new AppException(GateWayExceptionCodeMsg.USERNAME_ERROR);
    }

      if (!Objects.equals(theUser.getPassword(), user.getPassword())) {
      throw new AppException(GateWayExceptionCodeMsg.PASSWORD_ERROR);
    }

    //删除之前的token
    String preToken = (String) redisUtil.get(theUser.getUser_name());
    redisUtil.del(theUser.getUser_name());
    if (!StringUtils.isEmpty(preToken)) {
      redisUtil.del(preToken);
    }

    //生成token
    String token = jwtTokenUtil.generateToken(theUser.getUser_name());
    //将生成的token存进redis里面
    redisUtil.set(token, theUser.getUser_name(), Constants.TOKEN_EXPIRE);
    redisUtil.set(theUser.getUser_name(), token, Constants.TOKEN_EXPIRE);
    UserBean bean = new UserBean(theUser);
    bean.setToken(token);
    bean.setPassword("");

    return ResultBean.success(bean);
  }

  @GetMapping("/logout/{name}")
  public ResultBean userLogout(@PathVariable("name") String userName) {
    if (StringUtils.isEmpty(userName)) {
      throw new AppException(GateWayExceptionCodeMsg.USERNAME_EMPTY);
    }

    User user = userMapper.selectByUsername(userName);

    if (user != null) {
      String token = (String) redisUtil.get(user.getUser_name());
      redisUtil.del(user.getUser_name());
      if (!StringUtils.isEmpty(token)) {
        redisUtil.del(token);
      }
    }

    return ResultBean.success();
  }




  @PostMapping("/addUser")
  public ResultBean addUser(@RequestBody UserBean user){
    if(StringUtils.isEmpty(user.getCurUserName())){
      throw new AppException(GateWayExceptionCodeMsg.CUR_USER_EMPTY);
    }else{
      User opUser = userMapper.selectByUsername(user.getCurUserName());
      if(opUser==null){
        throw new AppException(GateWayExceptionCodeMsg.CUR_USER_NOT_EXIST);
      }else if(opUser.getIs_super()!=0){
        throw new AppException(GateWayExceptionCodeMsg.INSUFFICIENT_PERMISSIONS);
      }
    }
    if(StringUtils.isEmpty(user.getUser_name())){
      throw new AppException(GateWayExceptionCodeMsg.USERNAME_EMPTY);
    }
    if(StringUtils.isEmpty(user.getPassword())){
      throw new AppException(GateWayExceptionCodeMsg.PASSWORD_EMPTY);
    }
    if(user.getLevel()==null||user.getLevel()<1||user.getLevel()>6){
      throw new AppException(GateWayExceptionCodeMsg.USER_LEVEL_ERROR);
    }
    User preUser = userMapper.selectByUsername(user.getUser_name());
    if(preUser!=null){
      throw new AppException(GateWayExceptionCodeMsg.USER_NAME_EXISTS);
    }
    //添加用户密码使用MD5加密
    user.setPassword(MD5Utils.inputPassToFormPass(user.getPassword()));
    return ResultBean.success(userService.addUser(user));
  }

}
